package gui;

import comm.OutputTask;
import structures.City;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: dna
 * Date: 27.04.13
 * Time: 14:19
 */
public class ClientWindow extends JFrame {

    private JButton addCity;
    private JButton removeCity;

    private JPanel labelPanel;
    private JPanel buttonPanel;

    private JScrollPane scrollPane;

    private Map<String, City> cities;
    private JLabel output;
    private OutputTask outputTask;

    public ClientWindow(Map<String, City> cities) throws HeadlessException {

        this.cities = cities;

        // main window
        setTitle("Weather Web Service Client");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(360,540));

        // add button
        addCity = new JButton("Add City");
        addCity.addActionListener(new AddCityListener());

        // remove button
        removeCity = new JButton("Remove City");
        removeCity.addActionListener(new RemoveCityListener());

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(addCity);
        buttonPanel.add(removeCity);

        labelPanel = new JPanel();
        labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
        labelPanel.setBorder(new EmptyBorder(5,5,5,5));
        labelPanel.setMinimumSize(new Dimension(330,420));

        scrollPane = new JScrollPane(labelPanel);
        scrollPane.setPreferredSize(new Dimension(350,420));
        scrollPane.setVerticalScrollBarPolicy(22);

        // message label
        output = new JLabel("Welcome!");

        getContentPane().setLayout(new BorderLayout());

        getContentPane().add(scrollPane, BorderLayout.SOUTH);
        getContentPane().add(buttonPanel, BorderLayout.CENTER);
        getContentPane().add(output, BorderLayout.NORTH);

        pack();
        setResizable(false);
    }

    public void setMessage(String txt){
        output.setText(txt);
    }

    public void updateCityList() {
        for (City city : cities.values()){

            if (city.getLname() == null){
                // the city is new -> create all the labels

                // new entry
                JPanel entry = new JPanel();
                entry.setBorder(BorderFactory.createLineBorder(Color.black, 1));
                entry.setLayout(new FlowLayout(FlowLayout.LEFT));

                // All labels
                JLabel icon;
                JLabel name;
                JLabel desc;
                JLabel tehu;

                // Image panel
                JPanel imagePanel = new JPanel();
                imagePanel.setLayout(new FlowLayout());

                try {
                    URL url = new URL(city.getWeatherImg());
                    Image image = ImageIO.read(url);
                    icon = new JLabel(new ImageIcon(image));
                } catch (IOException e) {
                    icon = new JLabel("...");
                }

                imagePanel.add(icon);

                // Data panel
                JPanel dataPanel = new JPanel();
                dataPanel.setLayout(new BorderLayout());

                name = new JLabel(city.getName());
                if (city.getWeatherDescr() != null)
                    desc = new JLabel(city.getWeatherDescr());
                else
                    desc = new JLabel("");
                if (city.getTemperature() != null && city.getHumidity() != null)
                    tehu = new JLabel(city.getTemperature() + "°C " + city.getHumidity() + "%");
                else
                    tehu = new JLabel("");

                dataPanel.add(name,BorderLayout.NORTH);
                dataPanel.add(desc,BorderLayout.CENTER);
                dataPanel.add(tehu,BorderLayout.SOUTH);

                entry.add(imagePanel);
                entry.add(dataPanel);

                labelPanel.add(entry);

                city.setLname(name);
                city.setLicon(icon);
                city.setLdescr(desc);
                city.setLtemphumy(tehu);

                city.setPanel(entry);
            }

        }

        labelPanel.revalidate();
    }

    public void setOutputTask(OutputTask outputTask) {
        this.outputTask = outputTask;
    }

    private class AddCityListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            AddCityPopUp winny = new AddCityPopUp(output, outputTask);
            winny.setVisible(true);
        }
    }

    private class RemoveCityListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            RemoveCityPopUp winny = new RemoveCityPopUp(cities,labelPanel,output,outputTask);
            winny.setVisible(true);
        }
    }
}
